﻿using MicroCloud.Utils;

namespace Corporation.Project.Hangfire
{
    /// <summary>
    /// 测试Hangfire作业
    /// </summary>
    public class TestHangfireJob
    {
        #region "字段"
        #region "私有只读-身份认证上下文"
        /// <summary>
        /// 私有只读-身份认证上下文
        /// </summary>
        private readonly IIdentityContract _identityContract;
        #endregion
        #region "私有只读-服务提供者"
        /// <summary>
        /// 私有只读-服务提供者
        /// </summary>
        private readonly IServiceProvider _provider;
        #endregion

        #endregion

        #region "属性"
        #region "获取 日志对象"
        /// <summary>
        /// 获取 日志对象
        /// </summary>
        protected ILogger Logger => _provider.GetLogger(GetType());
        #endregion

        #endregion

        #region "构造函数"
        #region "初始化一个测试Hangfire作业的新实例"
        /// <summary>
        /// 初始化一个测试Hangfire作业 <see cref="TestHangfireJob"/> 的新实例
        /// </summary>
        /// <param name="identityContract">身份认证上下文</param>
        /// <param name="provider">服务提供者</param>
        public TestHangfireJob(IIdentityContract identityContract, IServiceProvider provider)
        {
            _identityContract = identityContract;
            _provider = provider;
        }
        #endregion

        #endregion

        #region "方法"
        #region "获取用户数量"
        /// <summary>
        /// 获取用户数量
        /// </summary>
        public string GetUserCount()
        {
            List<string> list = new()
            {
                _identityContract.Users.Count().ToString(),
                _identityContract.GetHashCode().ToString()
            };
            return list.ExpandAndToString();
        }
        #endregion
        #region "锁定用户2"
        /// <summary>
        /// 锁定用户2
        /// </summary>
        /// <returns></returns>
        public async Task<string> LockUser2()
        {
            List<string> list = new();
            UserManager<User> userManager = _provider.GetRequiredService<UserManager<User>>();
            User user2 = await userManager.FindByIdAsync("2");
            if (user2 != null)
            {
                list.Add($"user2.IsLocked: {user2.IsLocked}");
                user2.IsLocked = !user2.IsLocked;
                //注释更新代码，本方法仅做代码参考
                //await userManager.UpdateAsync(user2);
                //IUnitOfWork unitOfWork = _provider.GetUnitOfWork<User, int>();
                //unitOfWork.Commit();
                //user2 = await userManager.FindByIdAsync("2");
                //list.Add($"user2.IsLocked: {user2.IsLocked}");
            }

            return list.ExpandAndToString();
        }
        #endregion

        #region "同步测试方法"
        /// <summary>
        /// 同步测试方法
        /// </summary>
        /// <returns></returns>
        public void Test()
        {
            //TODO：Your Coding...
            //Logger.LogDebug(I18N.T("HangfireTest", "Hangfire Test：{0}", false, DateTime.Now.ToString()));
            Console.WriteLine(I18N.T("Hangfire Test：{0}", DateTime.Now.ToString()));
        }
        #endregion
        #region "异步测试方法"
        /// <summary>
        /// 异步测试方法
        /// </summary>
        /// <returns></returns>
        public async Task TestAsync()
        {
            //TODO：Your Coding...

            //UserManager<User> userManager = _provider.GetRequiredService<UserManager<User>>();
            //var hhh = userManager.Users.Where(o => o.Id == 433573375733829).Select(s => s.Id.CastTo<long?>()).ToList();
            //var hhhh = userManager.Users.Where(o => hhh.Contains(o.UpdaterId)).ToList();

            //Logger.LogDebug(I18N.T("HangfireTestAsync", "Hangfire TestAsync：{0}", false, DateTime.Now.ToString()));
            Console.WriteLine(I18N.T("Hangfire TestAsync：{0}", DateTime.Now.ToString()));

            await Task.CompletedTask;
        }
        #endregion

        #endregion

    }
}